package com.sskj.boom.ui.activity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.sskj.boom.R2;
import com.sskj.boom.adapter.ExpandAdapter;
import com.sskj.boom.adapter.Level0Item;
import com.sskj.boom.adapter.Level1Item;
import com.sskj.boom.bean.HelpBean;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.RxSchedulersHelper;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.boom.presenter.HelpCenterPresenter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sskj.boom.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author Hey
 * Create at  2019/07/08
 */
@Route(path = RConfig.BOOM_ACTIVITY_FAQ)
public class HelpCenterActivity extends BaseActivity<HelpCenterPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.tv_title)
    TextView tvTitle;

    private ExpandAdapter expandAdapter;
    private ArrayList<MultiItemEntity> list = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.boom_activity_help_center;
    }

    @Override
    public HelpCenterPresenter getPresenter() {
        return new HelpCenterPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.boom_helpCenterActivity1));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        expandAdapter = new ExpandAdapter(list);
        recyclerView.setAdapter(expandAdapter);

    }

    @Override
    public void initData() {
        /*1中文 2英文*/
        mPresenter.getFAQ("1");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, HelpCenterActivity.class);
        context.startActivity(intent);
    }

    public void getFaqOK(List<HelpBean> data) {
        if (data == null) {
            return;
        }
        Observable.fromArray(data)
                .map(new Function<List<HelpBean>, List<MultiItemEntity>>() {
                    @Override
                    public List<MultiItemEntity> apply(List<HelpBean> helpBeans) throws Exception {

                        Level0Item level0Item;
                        Level1Item level1Item;
                        for (int i = 0; i < helpBeans.size(); i++) {
                            level0Item = new Level0Item(helpBeans.get(i).getTitle());
                            level1Item = new Level1Item(helpBeans.get(i).getContent());
                            level0Item.addSubItem(level1Item);
                            list.add(level0Item);
                        }
                        return list;
                    }
                }).compose(RxSchedulersHelper.io_main())
                .subscribe(new Consumer<List<MultiItemEntity>>() {
                    @Override
                    public void accept(List<MultiItemEntity> multiItemEntities) throws Exception {
                        expandAdapter.notifyDataSetChanged();
                    }
                });

    }
}
